<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="适用课程" prop="courseId">
        <el-select v-model="queryParams.courseId" clearable filterable placeholder="请输入课程名选择">
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.name + '('+item.code+')'"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="教材名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入教材名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="适用课程" align="center" prop="course.name" />
      <el-table-column label="教材名称" align="center" prop="name" />
      <el-table-column label="创建教师" align="center" prop="user.nickName" />
      <el-table-column label="创建教师" align="center" prop="user.nickName" />
      <el-table-column label="最后更新时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.updateTime == null">{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-if="scope.row.updateTime != null">{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="320">
        <template slot-scope="scope">
          <router-link :to="'/teach/book/content/' + scope.row.id" class="link-type">
             <i class="el-icon-edit-outline"></i>&nbsp;制作&nbsp;
          </router-link>
          <router-link :to="'/teach/book/view/' + scope.row.id" class="link-type">
             <i class="el-icon-view"></i>&nbsp;预览&nbsp;
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教材对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="适用课程" prop="courseId">
          <el-select v-model="form.courseId" clearable filterable placeholder="请输入课程名选择">
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.name + '('+item.code+')'"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教材名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入教材名称" />
        </el-form-item>
        <el-form-item label="编写人员" prop="users">
          <el-select v-model="form.users" clearable filterable multiple placeholder="请输入姓名选择">
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.nickName + '('+item.userName+')'"
              :value="item.userId"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMakeBook, getBook, delBook, addBook, updateBook, exportBook } from "@/api/teach/book";
import { selectCourse} from "@/api/teach/course";
import { selectUser } from "@/api/system/user";

export default {
  name: "MyBook",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教材表格数据
      bookList: [],
      // 课程数据
      courseList: [],
      // 用户选项
      userOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseId: null,
        name: null,
        orderByColumn:"t.id",
        isAsc:"DESC"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseId: [
          { required: true, message: "适用课程不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "教材名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    selectCourse().then(response => {
      this.courseList = response.rows;
    });
    selectUser().then(response => {
      this.userOptions = response.rows;
    });
  },
  methods: {
    /** 查询教材列表 */
    getList() {
      this.loading = true;
      listMakeBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseId: null,
        name: null,
        comment: null,
        users:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教材";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBook(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教材";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBook(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除教材编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBook(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教材数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBook(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
